package com.ht.medicaltreatment.viewmodel

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.ToastUtils
import com.ht.medicaltreatment.activity.CentrifugeActivity
import com.ht.medicaltreatment.bean.AllCentrifugeBean
import com.ht.medicaltreatment.bean.CentrifugeDetailBean
import com.ht.medicaltreatment.bean.CentrifugeStateBean
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.launch

class CentrifugeMainViewModel():BaseViewModel() {
    private lateinit var activity: CentrifugeActivity
    fun bindActivity(activity: CentrifugeActivity){
        this.activity = activity
    }

    //停止离心-继续离心
    fun stopOrStart(stopCentrifugeId :String,onSuccess:(Int)->Unit){
        viewModelScope.launch {
            val bean = mApi.stopOrStart(stopCentrifugeId)
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data!!)
                is ApiResult.Error -> {
                    activity.dismissProgress()
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }

    //结束离心
    fun overCentrifuge(centrifugeId :String,onSuccess:()->Unit){
        activity.showProgress()
        viewModelScope.launch {
            val bean = mApi.overCentrifuge(centrifugeId)
            when(bean){
                is ApiResult.Success -> {
                    onSuccess()
                    activity.dismissProgress()
                }
                is ApiResult.Error -> {
                    activity.dismissProgress()
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }

    //查询所有离心机
    fun findAllCentrifuge(onSuccess:(MutableList<AllCentrifugeBean>?)->Unit){
        viewModelScope.launch {
            val bean = mApi.findAllCentrifuge()
            when(bean){
                is ApiResult.Success -> {
                    val data = bean.data.data
                    onSuccess(data)
                }
                is ApiResult.Error -> {
                    dismissProgress(bean.errorMessage)
                }
            }
        }
    }

    //根据id查询离心机信息
    fun findCentrifugeById(id:String,onSuccess:()->Unit, onError:()->Unit){
        viewModelScope.launch {
            val bean = mApi.findCentrifugeById(id)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> onError()
            }
        }
    }

    //停止离心
    fun stopCentrifuge(nowDate:String,remainderSecond:String,stopCentrifugeId:String,onSuccess:()->Unit){
        viewModelScope.launch {
            val bean = mApi.stopCentrifuge(nowDate,remainderSecond,stopCentrifugeId)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> {
                    activity.dismissProgress()
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }

    //停止离心-重新开始
    fun restartCentrifuge(stopCentrifugeId  : String,onSuccess:()->Unit){
        viewModelScope.launch {
            val bean = mApi.restartCentrifuge(stopCentrifugeId)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> {
                    activity.dismissProgress()
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }

    //开始离心，保存离心记录 和离心机中的样本关联
    fun saveCentrifugeOrder(stopCentrifugeId : String,time : String,onSuccess:()->Unit){
        viewModelScope.launch {
            val bean = mApi.saveCentrifugeOrder(stopCentrifugeId,time)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> {
                    activity.dismissProgress()
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }

    //离心完成，修改离心订单的状态和离心样本的状态（修改后）
    fun centrifugeState(centrifugeId : String,onSuccess:()->Unit){
        activity.showProgress()
        viewModelScope.launch {
            val bean = mApi.centrifugeState(centrifugeId)
            when(bean){
                is ApiResult.Success -> {
                    onSuccess()
                    activity.dismissProgress()
                }
                is ApiResult.Error -> {
                    activity.dismissProgress()
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }

    //根据离心机id查询当前离心的样本以及是否离心完成等
    fun centrifugeInfo(id:String, onSuccess:(CentrifugeDetailBean?)->Unit){
        viewModelScope.launch {
            val bean = mApi.centrifugeInfo(id)
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data)
                is ApiResult.Error -> {
                    activity.dismissProgress()
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }

    //查询所有离心机状态
    fun centrifugeState(onSuccess:(MutableList<CentrifugeStateBean>)->Unit){
        viewModelScope.launch {
            val bean = mApi.centrifugeState()
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data!!)
                is ApiResult.Error -> {
                    activity.dismissProgress()
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }
}